﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;

namespace SerialPortDal
{
    public static class WorkGroupDAO
    {
        public static int save(WorkGroup obj)
        {
            String cmd = "insert into WORK_GROUP(group_name,notes) values(?,?)";
            SQLiteParameter[] pas = {
                new SQLiteParameter("group_name",obj.Group_name),
                new SQLiteParameter("notes",obj.Notes)
                 };
            return SqliteHelper.ExecuteNonQuery(cmd, CommandType.Text, pas);
        }

        public static WorkGroup selectObjByName(string groupName)
        {
            SQLiteParameter[] pas = { new SQLiteParameter("group_name", groupName) };
            DataTable tbl = SqliteHelper.ExecuteDataTable("select * from work_group WHERE group_name=?", CommandType.Text, pas);
            if (tbl == null || tbl.Rows.Count == 0)
            {
                return null;
            }
            return parseRow2Obj(tbl.Rows[0]);
        }

        public static List<WorkGroup> getAll()
        {
            DataTable tbl = SqliteHelper.ExecuteDataTable("select * from work_group");
            List<WorkGroup> objList = new List<WorkGroup>();
            foreach (DataRow r in tbl.Rows)
            {
                objList.Add(parseRow2Obj(r));
            }
            return objList;
        }


        private static WorkGroup parseRow2Obj(DataRow r)
        {
            WorkGroup obj = new WorkGroup();
            obj.Group_no = Convert.ToInt32(r["group_no"]);
            obj.Group_name = r["group_name"] as string;
            obj.Notes = r["notes"] as string;
            return obj;
        }
    }
}
